**********************

* Spatial Replication

**********************

********************************
* SPMAT test for autocorrelation
********************************

* Spatial matrix

clear

clear mata

* net install shp2dta.pkg 
* ssc install shp2dta 

shp2dta using COL_ADM2, database(COL_ADM2db) coordinates(coords) genid(id)

use COL_ADM2db

sort id

merge id using colspat.dta

drop if _merge!=3

save COL_ADM2db, replace

* net install spmap.pkg

spmap using coords, id(id)

use COL_ADM2db, clear

spmat contiguity colspatial using coords, id(id) normalize(row) saving(colspatial)

spmat summarize colspatial, links

save COL_ADM2db.dta, replace

****** Now convert this to spatwmat to do autocorrelation tests ******
*Note sufficient mata/matsize needed

clear
clear matrix
clear mata
set max_memory .
set more off

use COL_ADM2db.dta

spmat contiguity colspatialtest using "coords.dta", id(id) replace 

spmat summarize colspatialtest 

spmat export colspatialtest using "colspatialtest.txt", noid replace 

clear

clear mata
set maxvar 32767 

import delimited using "colspatialtest.txt", delim(" ")

drop in 1 

save "colspatialtest.dta", replace 

set matsize 11000

*net install xsmle.pkg

spatwmat using "colspatialtest.dta", name(Ws) standardize 

*** Run the tests for the different imputation datasets, created via R script included ***

use Imputed1_51318.dta
*use Imputed2_51318.dta
*use Imputed3_51318.dta
*use Imputed4_51318.dta
*use Imputed5_51318.dta

collapse (max) AUC drugs meta caqueta putumayo guaviare (mean) disbogota ///
pop_log tribut_2_log_sq tribut_2_log (sum) pop_attacks, by (id)

sort id

* Incomplete annual data observations are dropped, matrix needs to be of equal size

drop if id==.
drop if id==207 
drop if id==809
drop if id==877 
drop if id==191 


reg AUC tribut_2_log tribut_2_log_sq pop_attacks disbogota drugs pop_log caqueta meta putumayo guaviare


* Test for spatial dependency in the fitted regression
spatdiag, weights(Ws)
  
* Global autocorrelation
spatgsa AUC tribut_2_log tribut_2_log_sq pop_attacks disbogota drugs pop_log caqueta meta putumayo guaviare, weights(Ws) moran

* Local autocorrelation
spatlsa AUC, weights(Ws) moran id(id) sort


******* spmat will be used from now on, spatwmat was only for tests ********

clear

use COL_ADM2db.dta

clear matrix

spmat contiguity colspatial2 using coords, id(id) normalize(row)

spmat eigenvalues colspatial2

spmat summarize colspatial2, links

spmat save colspatial2 using colspatial2.spmat, replace

clear

*** Run models for different imputations ***

*these datasets are produced by the R script submitted for replication 

use Imputed1_51318.dta
*use Imputed2_51318.dta
*use Imputed3_51318.dta
*use Imputed4_51318.dta
*use Imputed5_51318.dta

drop if id==.
drop if id==207 
drop if id==809
drop if id==877 
drop if id==191 

xtset id ano

spmat use W using "colspatial2.spmat"

*** Table 15 replication (TOTAL EFFECTS) ***
*net install xsmle.pkg

* Spatial durbin basic model (M1)
xsmle AUC tribut_2_log tribut_2_log_sq pop_attacks disbogota drugs pop_log caqueta meta putumayo guaviare, wmat(colspatial2) model(sdm) effects 

* Spatial durbin year FE (M2)
xsmle AUC tribut_2_log tribut_2_log_sq pop_attacks disbogota drugs pop_log caqueta meta putumayo guaviare, wmat(colspatial2) model(sdm) fe type(time) effects

* Spatial durbin year RE (M3)
xsmle AUC tribut_2_log tribut_2_log_sq pop_attacks disbogota drugs pop_log caqueta meta putumayo guaviare, wmat(colspatial2) model(sdm) re type(time) effects
